media_manager.admin = liwe.module ( "media_manager.admin" );

media_manager.admin.templates = {
		"image-preview"  : '<img src="/site/media_manager/image/%(_mode)s/%(id_media)s.jpg" alt="%(descr)s" title="%(descr)s" border="0" /><br /><a href="javascript:media_manager.admin.del(\'%(id_media)s\')">Elimina</a>',
		"image-render"  : '<img src="/site/media_manager/image/full/%(id_media)s.jpg" alt="%(descr)s" title="%(descr)s" border="0" /><br /><a href="javascript:media_manager.admin.del(\'%(id_media)s\')">Elimina</a>',
		/*
		"image-row"  : '<table border="1" width="400">' + 
			       '<tr><td><img src="/site/media_manager/image/icon/%(id_media)s.jpg" alt="%(descr)s" title="%(descr)s" border="0" width="64"/></td>' + 
				'<td>%(descr)s</td>' + 
				'<td nowrap="nowrap" width="120">%(_acts)s</td>' + 
				'</tr></table>',
		*/
		"image-row" :  '<div id="media-table" class="media-table-image"><table border="0" width="400">' +
                               '<tr><td class="media-grid-image" style="width:24%"><img src="/site/media_manager/image/icon/%(id_media)s.jpg" alt="%(descr)s" title="%(descr)s" border="0" width="64" height="64"/></td>' +
                               '<td style="width:40%" class="media-grid-descr">%(descr)s</td>' +
                               '<td nowrap="nowrap" width="120">%(_acts)s</td>' +
                               '</tr></table></div>',

		"youtube-row" :'<div id="media-table" class="media-table-youtube"><table border="0" width="400">' +
                               '<tr><td class="media-grid-descr" style="width:24%">YouTube video</td>' +
                               '<td style="width:40%" class="media-grid-descr"><a href="http://www.youtube.com/watch?v=%(data)s" target="_blank">%(descr)s</a></td>' +
                               '<td nowrap="nowrap" width="120">%(_acts)s</td>' +
                               '</tr></table></div>',

		"youtube-acts": '<div class="mm-act-del" onclick="media_manager.admin.del(\'%(id_media)s\')" title="Elimina"></div>',

		"image-acts" :  '<div class="mm-act-rot-l" onclick="media_manager.admin.rotate(\'%(id_media)s\',\'left\')" title="Ruota a sinistra"></div>' + 
				'<div class="mm-act-rot-r" onclick="media_manager.admin.rotate(\'%(id_media)s\',\'right\')" title="Ruota a destra"></div>' + 
				'<div class="mm-act-del" onclick="media_manager.admin.del(\'%(id_media)s\')" title="Elimina"></div>',

		"youtube-preview"  : '<img src="/site/media_manager/youtube/%(_mode)s/%(id_media)s.jpg" alt="%(descr)s" title="%(descr)s" border="0" /><br /><a href="javascript:media_manager.admin.del(\'%(id_media)s\')">Elimina</a>',
		"youtube-render" : '<object width="618" height="500"><param name="movie" value="http://www.youtube.com/v/%(data)s&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/%(data)s&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="618" height="500"></embed></object><br /><a href="javascript:media_manager.admin.del(\'%(id_media)s\')">Elimina</a>',
		"flash-preview" : '<object width="200" height="200"><param name="movie" value="/site/media_manager/flash/orig/%(id_media)s.swf"></param><embed src="/site/media_manager/flash/orig/%(id_media)s.swf" type="application/x-shockwave-flash" width="200" height="200"></embed></object><br /><a href="javascript:media_manager.admin.del(\'%(id_media)s\')">Elimina</a>',
		"flash-render" : '<object width="200" height="200"><param name="movie" value="/site/media_manager/flash/orig/%(id_media)s.swf"></param><embed src="/site/media_manager/flash/orig/%(id_media)s.swf" type="application/x-shockwave-flash" width="200" height="200"></embed></object><br /><a href="javascript:media_manager.admin.del(\'%(id_media)s\')">Elimina</a>'
};

/*
media_manager.admin.init = function ()
{
	product.admin.add_tab ( "Media", "tab_media", media_manager.admin._prepare_media );
};
*/

media_manager.admin.init = function ()
{
	media_manager.admin._dest_id = null;
};

media_manager.admin.render = function ( dest_id, module, id_obj )
{
        var f = new liwe.form.instance ( "media-form" );
	var dg;

	if ( ! dest_id ) dest_id = media_manager.admin._dest_id;
	if ( ! id_obj ) id_obj = "";

	media_manager.admin._dest_id = dest_id;
        
       // f.text ( { label: "Codice", name: "id_obj", onchange: "media_manager.admin.get_info(this.value)", size: 20, maxsize: 50, mandatory: true, value: id_obj } );
	f.hidden ( "module", module );
	f.button ( { value: "Mostra", "class" :"grey", onclick: "media_manager.show('" + module + "','" + id_obj + "',0, 900 )"  } );
	f.button ( { value: "YouTube+", "class" : "red",onclick: "media_manager.admin._new_item('youtube')", nonl: true } );
	f.button ( { value: "Immagine+", "class" : "blue",onclick: "media_manager.admin._new_item('image')", nonl: true } );
	f.button ( { value: "Flash+", "class" :"yellow",onclick: "media_manager.admin._new_item('flash')" } );
	dg = f.dgrid ( { name: "media-grid", height: "250", colspan: 7 } );
        f.set ( dest_id );

	dg.events [ 'change' ] = media_manager.admin._order_changed;

	media_manager.admin.set_id_obj ( module, id_obj );
};        

media_manager.admin._order_changed = function ( dg )
{
	var lst = dg.get_list ();
	var dst = [];
	var pos = 0;

	lst.iterate ( function ( elem ) 
	{
		dst.push ( elem.data [ 'id_media' ] );
		dst.push ( pos ++ );
	} );

	media_manager.admin.ajax ( { action: "media_manager.admin.multi_repos", repos: '|'.join ( dst ) } );
};

media_manager.admin._grid_cell_changed = function ( grid, x, y, new_val )
{
	console.debug ( "Grid: %o - x: %d - y: %d - new_val: %s", grid, x, y, new_val );
};

media_manager.admin.set_id_obj = function ( module, id_obj )
{
	var f = liwe.form.get ( "media-form" );

	f.set_value ( "id_obj", id_obj );

	media_manager.admin._id_obj = id_obj;
	media_manager.admin._module = module;

        if ( id_obj )
	{
                media_manager.get_items ( module, id_obj, function ( items ) { media_manager.admin.items = items; media_manager.admin.list_items (); }, media_manager.admin.templates, true );
	}
};

media_manager.admin._new_item = function ( kind )
{
	media_manager.admin.edit_item ( -1, kind );
};

media_manager.admin._grid_row_select = function ( grid, row_num, is_selected )
{       
        if ( ! is_selected ) return;

	media_manager.admin.edit_item ( row_num );
};

media_manager.admin.edit_item  = function ( row_num, kind )
{
	var s = '<div id="mm-admin-edit"><div id="mm-admin-edit-form">form</div><div id="mm-admin-edit-obj"><!-- dati --></div></div>';
	var f = new liwe.form.instance ( "mm-admin-edit" );
	var item, action, title;

	f.events [ 'complete' ] = "media_manager.admin._edit_done";

	if ( row_num >= 0 )
	{
		title = 'Modifica Elemento';
		item = media_manager.admin.items [ row_num ];
		f.hidden ( "action", "media_manager.admin.edit" );
		f.hidden ( "id_media", item.data [ 'id_media' ] );
	} else {
		title = 'Nuovo elemento';
		item = { "data" : { "id_media" : 0, "kind" : kind } };
		f.hidden ( "action", "media_manager.admin.add" );
		f.hidden ( "id_obj", media_manager.admin._id_obj );
		f.hidden ( "module", media_manager.admin._module );
		f.hidden ( "kind", kind );
	}

	f.text ( { label: "Descr", name: "descr", value: item.data [ 'descr' ], size: 40, maxlength: 200 } );
	// removed -> we have drag 'n' drop
	//f.number ( { label: "pos", name: "pos",   value: item.data [ 'pos' ], size: 5, maxlength: 5 } );
	f.hidden ( "__PYHP_SESSION", liwe.cookie.get ( "session" ) );

	var _ytcheck = function ()
	{
		var value = f.get_value ( 'data' );
		if ( /^[a-zA-Z0-9\-_]{11}$/.test ( value ) ) return;
		var re = /([\?&]v=|\/v\/|youtu.be\/)([a-zA-Z0-9\-_]{11})/;
		var res = value.match ( re );
		value = res && res.length == 3 ? res [ 2 ] : '';
		f.set_value ( 'data', value );
	};

	switch ( item.data [ 'kind' ] )
	{
		case "youtube":
			f.text ( { label: "URL", name: "data", value: item.data [ 'data' ], onblur : _ytcheck } );
			break;

		case "image":
		case "flash":
			f.uploader ( { label: "URL", name: "data", type: 0 } );
			break;
	}

	f.submit ( { value: "Invia" } );

	liwe.lightbox.fade = false;
	liwe.lightbox.easy ( "mm-lightbox", title, 500, 400 );
	$( "mm-lightbox", s );

	if ( row_num >= 0 ) $( "mm-admin-edit-obj", item.toString () );
	f.set ( "mm-admin-edit-form" );
};

media_manager.admin._edit_done = function ()
{
	liwe.lightbox.close (); 
	media_manager.admin.render ( null, media_manager.admin._module, media_manager.admin._id_obj );
};


media_manager.admin.list_items = function ()
{
	// console.debug ( "Media-Manager Items: %o", media_manager.admin.items );
	var dg = DGrid.get ( "media-grid" );
	var items = media_manager.admin.items;
	var t, l = items.length, m, acts;
	var templ, s;

	dg.clear ();

	for ( t = 0; t < l; t ++ )
	{
		m = items [ t ];
		templ = media_manager.admin.templates.get ( m.data [ 'kind' ] + "-row", "Template: " + m.data [ 'kind' ] + "row" );

		m.data [ '_acts' ] = String.formatDict ( media_manager.admin.templates.get ( m.data [ 'kind' ] + "-acts", "ACTS" ), m.data );

		s = String.formatDict ( templ, m.data );
		dg.add ( s, m );
	}

	dg.render ();
};

media_manager.admin._cell_apply = function ( row_num )
{
	var grid = OS3Grid.get ( "media-grid" );
	var row = grid.get_row ( row_num );

	row [ 'action' ] = 'media_manager.admin.edit';
	row [ 'id_media' ] = row [ 0 ];
	row [ 'descr' ] = row [ 2 ];
	row [ 'pos' ] = row [ 4 ];
	row [ 'data' ] = row [ 3 ];

	media_manager.admin.ajax ( row, function ( v ) { alert ( "Riga aggiornata con successo!" ); } );
};

/*
media_manager.admin.add_image = function ()
{
	var f = new liwe.form.instance ( "mm-image" );

	f.hidden ( "action", "media_manager.admin.add_image" );
	f.upload ( { label: "Image", name: "img" } );
	f.text ( { label: "Descr", name: "descr", size: 50, maxlength: 200 } );
	f.submit ( { value: "Send" } );
	f.set ( "block_main" );
};
*/

media_manager.admin.del = function ( id_media )
{
	if ( ! confirm ( "Confermi la cancellazione di questo elemento?" ) ) return;

	media_manager.admin.ajax ( { action: "media_manager.admin.del_media", id_media: id_media }, function ( v )
		{
			media_manager.admin._update_items ( v.media_items );
			media_manager.show ( media_manager.admin._module, media_manager.admin._id_obj, 0, 900, false );
		} );
};

media_manager.admin.rotate = function ( id_media, direction )
{
	if ( ! confirm ( "Ruotare questo elemento?" ) ) return;

	media_manager.admin.ajax ( { action: "media_manager.admin.rotate", direction: direction, id_media: id_media }, function ( v )
		{
			alert ( "Rotazione effettuata. Per vedere le modifiche e' necessario ricaricare la pagina web." );
		} );
};

media_manager.admin._update_items = function ( v )
{
	media_manager.admin.items = media_manager._set_items ( v, media_manager.admin._module, media_manager.admin._id_obj, media_manager.admin.templates, null );
	media_manager.admin.list_items ();
};

/* JCONED PLUGIN FUNCTIONS */
/*
media_manager.admin._jconed_register_handler = function ( data ) 
{
	media_manager.admin._jconed_show_media ();
};
media_manager.admin._render_items = function ( items )
{
	console.debug ( "\n MEDIA_MANAGER - jconed plug-in : items = %o\n\n", items );

	//var img_thumbs =  media_manager._render_icons ( items, 'icon', 'start-view', 10 );
	var f = new liwe.form.instance ( 'jconed-images-form', 'no-action' ); 
 
	f.hidden ( '_module', 'media_manager' );
	f.hidden ( '_title', 'Images plug-in' );

	f.descr ( 'image-thumbs', img_thumbs );

	f.button ( { value: "Conferma", nonl : true, onclick: "jconed.render_plugin('jconed-images-form')" } );

	jconed.toolbar.set_context ( f.get () );
}

media_manager.admin._jconed_show_media = function ( frm_data ) 
{

	var view_opts = [ { 'label' : 'icons', 'value' : 'icons' }, { 'label' : 'list', 'value' : 'details' } ];
	var module = 'media_manager', id_obj = 0, chk_fields = false;
	
	var params = jconed.editor.current.get ( 'params' );
	if ( params && params.get ( 'form_inst' ) )
	{
		var f = liwe.form.get ( params [ 'form_inst' ] );
		module = f.get_value ( 'jconed_module' );
		id_obj = f.get_value ( 'jconed_id' );
		if ( module && id_obj ) chk_fields = true;
	}

	if ( ! chk_fields ) console.warn ( "\nJCONED media_manager plug-in: jconed_module and jconed_id form fields are missing !! " );


	media_manager.get_items ( module, id_obj, media_manager.admin._render_items, templates, force );
};

media_manager.admin._jconed_context = function ()
{
        var item = jconed.get_plugin_selection ();
        if ( ! item ) return;
        if ( ! liwe.dom.has_class ( item, 'liwe-plugin-media_manager' ) ) return;

        pre = item.getElementsByTagName ( "pre" );
        if ( ! pre.length ) return;

        pre = pre [ 0 ];

        var values = null;
        eval ( 'values = ' + pre.innerHTML );

        media_manager.admin._jconed_show_media ( values [ 'params' ] );
};
*/
media_manager.admin._set_form_url = function ( id_media, mode )
{
	var url = String.format ( '/site/media_manager/image/%s/%s.jpg', mode, id_media );
	var f = liwe.form.get ( 'jconed-image' );
	f.set_value ( 'image', url );
	liwe.lightbox.close ();
	return false;
};
/*
media_manager.admin.init = function ()
{
        var _acts = [];

        var _plugin = [
                {
                        label:   "Images",
                        handler: media_manager.admin._jconed_register_handler,
                        context: media_manager.admin._jconed_context
                }
        ];

        media_manager.admin.register ( _acts, _plugin );
};
*/
